/**
 * @author 黄波
 * 创建日期：2011Oct 2, 2011
 * TODO// N/A
 */
package com.platform.user.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Properties;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.platform.constant.ResponseCode;
import com.platform.os.util.Mail;
import com.platform.pojo.UserInfo;
import com.platform.user.bo.UserBo;

/**
 * 锁定用户
 */
public class LockUserAction extends UserSupport {

	private String id;	//用户id
	private String lockStatus;	//锁定状态
	private String msg;
	private boolean success;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public boolean isSuccess() {
		return success;
	}
	public void setSuccess(boolean success) {
		this.success = success;
	}
	@Override
	public String execute() throws Exception {
		if(id == null || id.equals("")) {
			msg = ResponseCode.FAILURE_ID_NULL;
		} else {
			UserBo bo = UserBo.getInstance();
			final UserInfo user = bo.getUser(id);
			if(user == null) {
				msg = ResponseCode.FAILURE_NO_DATA;
			} else {
				user.setIsAudit(Integer.parseInt(lockStatus));
				if(user.getIsAudit() == 0) {
//					new Thread() {
//						public void run() {
//							try {
//								String systemProfile = "/system.properties";
//								InputStream stream = new FileInputStream(new File(ServletActionContext.getServletContext().getRealPath("/") + systemProfile));
//								Properties properties = new Properties();
//								properties.load(stream);
//								stream.close();
//								Mail.sendMail("您的帐户被锁定-南京化工职业技术学院资源共享平台！", "<br/>请通知管理员解锁。<a href='" + properties.getProperty("server_static_addr") + "' style='color:red;'>点击这里，进行访问</a>！<br/><br/>---------------<br/>南京化工职业技术学院--资源共享平台<br/>", user.getEmail());
//							} catch (FileNotFoundException e) {
//								// TODO Auto-generated catch block
//								e.printStackTrace();
//							} catch (IOException e) {
//								// TODO Auto-generated catch block
//								e.printStackTrace();
//							}
//						}
//					}.start();
				}
				if(user.getIsAudit() == 1) {
//					new Thread() {
//						public void run() {
//							try {
//								String systemProfile = "/system.properties";
//								InputStream stream = new FileInputStream(new File(ServletActionContext.getServletContext().getRealPath("/") + systemProfile));
//								Properties properties = new Properties();
//								properties.load(stream);
//								stream.close();
//								Mail.sendMail("您的帐户已解锁-南京化工职业技术学院资源共享平台！", "<br/><a href='" + properties.getProperty("server_static_addr") + "' style='color:red;'>点击这里，进行访问</a>！<br/><br/>---------------<br/>南京化工职业技术学院--资源共享平台<br/>", user.getEmail());
//							} catch (FileNotFoundException e) {
//								// TODO Auto-generated catch block
//								e.printStackTrace();
//							} catch (IOException e) {
//								// TODO Auto-generated catch block
//								e.printStackTrace();
//							}
//						}
//					}.start();
				}
				if(bo.updateUser(user)) {
					msg = ResponseCode.SUCCESS_UPDATE;
				} else {
					msg = ResponseCode.FAILURE_DB_ERROR;
				}
			}
		}
		

		success = true;
		HashMap map = new HashMap();
		map.put("msg", msg);
		map.put("success", success);
		JSONObject json = JSONObject.fromObject(map);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		out.println(json);
		out.flush();
		out.close();
		
		return null;
	}
	public String getLockStatus() {
		return lockStatus;
	}
	public void setLockStatus(String lockStatus) {
		this.lockStatus = lockStatus;
	}
	
}
